package com.guangwei.wechatautoanswer

import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.Settings
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.listView
import java.io.BufferedReader
import java.io.InputStreamReader

class MainActivity : AppCompatActivity() {
    private var itemList = ArrayList<String>()
    private var adapter: MyAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        initListView()
    }

    override fun onRestart() {
        super.onRestart()
        initListView()
    }

    override fun onStart() {
        showNotification()
        super.onStart()
    }

    private fun initView() {
        btn_open_accessbility.setOnClickListener {
            startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS))
        }

        btn_open_wechat.setOnClickListener {
            val intent = packageManager.getLaunchIntentForPackage("com.tencent.mm")
            startActivity(intent)
        }

        btn_modify_name_list.setOnClickListener {
            val intent = Intent(this, NameListMaintain::class.java)
            startActivity(intent)
        }
    }

    private fun initListView(){
        itemList.clear()
        // Add the init data to the list below
        loadData()
        adapter = MyAdapter(itemList,this)
        listView.adapter = adapter
    }

    private fun loadData() {
        try {
            val fileInputStream = openFileInput("data.txt")

            //把文件内容读取进缓冲读取器（use方法会自动对BufferedReader进行关闭）
            BufferedReader(InputStreamReader(fileInputStream)).use {
                var line: String
                itemList.clear()
                while (true) {
                    line = it.readLine() ?: break //当有内容时读取一行数据，否则退出循环
                    println(line) //打印一行数据
                    itemList.add(line)
                    adapter = MyAdapter(itemList,this)
                    listView.adapter = adapter
                }
            }
        } catch (throwable: Throwable) {
            // 捕获到异常的处理
        }
    }


    @SuppressLint("PrivateResource")
    private fun showNotification() {
        val channelId = "channel_wechartautoanswer"
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name = getString(R.string.channel_name)
            val descriptionText = getString(R.string.channel_description)
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val channel = NotificationChannel(channelId, name, importance).apply {
                description = descriptionText
            }
            // Register the channel with the system
            val notificationManager: NotificationManager =
                getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            notificationManager.createNotificationChannel(channel)
        }

        // Create an explicit intent for an Activity in your app
        val intent = Intent(this, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }
        val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)

        val builder = NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.notification_icon_background)
            .setContentTitle("正在运行...")
            .setContentText("点击打开 微信来电自动接听")
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            // Set the intent that will fire when the user taps the notification
            .setContentIntent(pendingIntent)
            .setAutoCancel(false)

        val notification = builder.build()
        notification.flags = notification.flags or Notification.FLAG_ONGOING_EVENT

        with(NotificationManagerCompat.from(this)) {
            // notificationId is a unique int for each notification that you must define
            notify(1, notification)
        }
    }
}


